<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '详情'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    v-el-drag-dialog
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <!-- "id": 9,
    "productName": "韩版潮流短袖",
    "productImage": "https://axure-file.lanhuapp.com/abaa4a73-6a75-4a4c-a82a-8a1f06b0e0c3__fee28fe27963f7715fc5138c020895b5.png",
    "profit": 0.035,
    "unitPrice": 2000,
    "attributeList": [],
      "productSkuId": 52-->
      <!-- <el-form-item label="ID" prop="id" label-width="100px">
        <el-input v-model="dataForm.id" placeholder=""></el-input>
      </el-form-item>-->
      <el-form-item label="商品">
        <el-select v-model="dataForm.id" placeholder="商品" prop="id">
          <el-option
            v-for="item in goodsListItem"
            :label="item.productName"
            :key="item.id"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="商品名称" prop="productName" label-width="100px">
        <el-input v-model="dataForm.productName" placeholder="备注"></el-input>
      </el-form-item>
      <!-- <el-form-item label="商品图片" align="center">
        <template slot-scope="scope">
          <img :src="scope.row.productImage" width="40" height="40" class="head_pic" />
        </template>
      </el-form-item>-->
      <el-form-item label="每周收益" prop="profit" label-width="100px">
        <el-input v-model.number="dataForm.profit" placeholder="备注" label-width="100px"></el-input>
      </el-form-item>
      <el-form-item label="商品分期值" prop="unitPrice" label-width="100px">
        <el-input v-model.number="dataForm.unitPrice" placeholder="备注" label-width="100px"></el-input>
      </el-form-item>
       <el-form-item label="收益总期" prop="cycle" label-width="100px">
        <el-input v-model.number="dataForm.cycle" placeholder="备注" label-width="100px"></el-input>
      </el-form-item>
       <el-form-item label="购买价格" prop="totalPrice" label-width="100px">
        <el-input v-model.number="dataForm.totalPrice" placeholder="备注" label-width="100px"></el-input>
      </el-form-item>
      <el-form-item label="规格属性列表" prop="attributeList" label-width="100px">
        <el-input v-model.number="dataForm.attributeList" placeholder="备注" label-width="100px"></el-input>
      </el-form-item>
      <el-form-item label="商品skuID" prop="productSkuId" label-width="100px">
        <el-input v-model.number="dataForm.productSkuId" placeholder="备注" label-width="100px"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import mixtoggle from "@/commons/content/mixin/mixin.js";
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: "",
        productName: "",
        productImage: "",
        profit: "",
        attributeList: "",
        productSkuId: "",
        unitPrice: ""
      },
      dataRule: {
        paramKey: [
          { required: true, message: "参数名不能为空", trigger: "blur" }
        ],
        paramValue: [
          { required: true, message: "参数值不能为空", trigger: "blur" }
        ]
      },
      goodsListItem: []
    };
  },
  mixins: [mixtoggle],
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.getgoodsListItem();
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/productInvestment/investmentDetail/` + id
            ),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            // console.log(data)
            if (data && data.status === 200) {
              this.dataForm.id = data.data.id;
              this.dataForm.productName = data.data.productName;
              this.dataForm.productImage = data.data.productImage;
              this.dataForm.profit = data.data.profit;
              this.dataForm.productSkuId = data.data.productSkuId;
              this.dataForm.unitPrice = data.data.unitPrice;
              this.dataForm.attributeList = data.data.attributeList;
            }
          });
        }
      });
    },
    // 表单提交
    // dataFormSubmit() {
    //   this.$refs["dataForm"].validate(valid => {
    //     if (valid) {
    //       this.$http({
    //         url: this.$http.adornUrl(
    //           `/productInvestment/${
    //             !this.id ? "save" : "update/" + this.dataForm.id
    //           }`
    //           // /productInvestment/save/{userId}
    //         ),
    //         method: "post",
    //         data: this.$http.adornData({
    //           id: this.dataForm.id || undefined,
    //           cycle: this.dataForm.cycle,
    //           productId: this.dataForm.productId,
    //           productSkuId: this.dataForm.productSkuId,
    //           profit: this.dataForm.profit,
    //           totalPrice: this.dataForm.totalPrice

    //         })
    //       }).then(({ data }) => {
    //         if (data && data.status === 200) {
    //           this.$message({
    //             message: "操作成功",
    //             type: "success",
    //             duration: 1500,
    //             onClose: () => {
    //               this.visible = false;
    //               this.$emit("refreshDataList");
    //             }
    //           });
    //         } else {
    //           this.$message.error(data.message);
    //         }
    //       });
    //     }
    //   });
    // },

    
    // 提交表单
    dataFormSubmit() {
      this.$http({
        url: this.$http.adornUrl(
          `/productInvestment/${
            !this.id
              ? "save/" + this.user.data.id
              : "update/" + this.dataForm.id
          }`
        ),
        data: this.$http.adornData({
          productId: this.dataForm.id,
          cycle: this.dataForm.cycle,
          productSkuId: this.dataForm.productSkuId,
          profit: this.dataForm.profit,
          totalPrice: this.dataForm.totalPrice
        }),
        method: "post"
      }).then(({ data }) => {
            if (data && data.status === 200) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              console.log(data);
              this.$message.error(data.message);
            }
          })
    },
    getgoodsListItem() {
      this.$http({
        url: this.$http.adornUrl(`/product/list`),
        method: "get"
      }).then(({ data }) => {
        if (data && data.status === 200) {
          this.goodsListItem = data.data.list;
        } else {
          this.goodsListItem = [];
        }
      });
    }
  },
  mounted() {
    (async () => {
      // 组件创建的时候使用mixin来获取用户名
      await this.getUser();
    })();
  }
};
</script>
